package misc

import (
	"dhfshop/controllers/base"
	"dhfshop/models/consts"
	"dhfshop/models/misc"
	"github.com/astaxie/beego/validation"
	"time"
	//"github.com/astaxie/beego"
)

type NotifyController struct {
	base.BaseController
}

// 查询通知
func (this *NotifyController) GetNotify() {
	query, _, fields, order_by, offset, limit, err := this.GetQueryPara()
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	var allowStoreId []int64
	if this.CurrentUser != nil {
		allowStoreId = this.CurrentUser.AllowStoreId
	}
	//beego.Debug("allowStoreId:", allowStoreId)
	l, cnt, err := misc.GetAllNotifys(allowStoreId, query, fields, order_by, offset, limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		TotalCount int64          `json:"total_count"`
		Notifys    []*misc.Notify `json:"notifys"`
	}{TotalCount: cnt, Notifys: l}
	this.Data["json"] = r
	this.ServeJSON()
}

// 添加通知
func (this *NotifyController) AddNotify() {
	var v misc.Notify
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.TypeId, "type_id")
	valid.Required(v.Jump, "jump")
	valid.Required(v.StoreId, "store_id")
	valid.Required(v.Content, "content")
	valid.Required(v.StartAtStr, "start_at")
	valid.Required(v.EndAtStr, "end_at")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	v.StartAt, _ = time.Parse(consts.DATETIME_FORMAT, v.StartAtStr)
	v.EndAt, _ = time.Parse(consts.DATETIME_FORMAT, v.EndAtStr)
	id, err := misc.AddNotify(&v)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	v.NotifyId = int64(id)
	this.Data["json"] = v
	this.ServeJSON()
}

// 更新一个通知
func (this *NotifyController) UpdateNotify() {
	var v misc.Notify
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.NotifyId, "notify_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	updateFields, err := this.GetUpdateFields(v, "", "form", []string{"notify_id", "store_id"})
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	if len(v.StartAtStr) > 0 {
		v.StartAt, _ = time.Parse(consts.DATETIME_FORMAT, v.StartAtStr)
	}
	if len(v.EndAtStr) > 0 {
		v.EndAt, _ = time.Parse(consts.DATETIME_FORMAT, v.EndAtStr)
	}
	if len(updateFields) > 0 {
		err := misc.UpdateNotify(&v, updateFields...)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		NotifyId int64     `json:"notify_id"`
		Modified time.Time `json:"modified"`
	}{NotifyId: v.NotifyId, Modified: time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

// 删除一个通知
func (this *NotifyController) DeleteNotify() {
	var v misc.Notify
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.NotifyId, "notify_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	err := misc.DeleteNotify(v.NotifyId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		NotifyId int64     `json:"notify_id"`
		Modified time.Time `json:"modified"`
	}{NotifyId: v.NotifyId, Modified: time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}
